package com.itzxx.huazhun.ui.device.list

import android.text.TextUtils
import com.itzxx.huazhun.data.model.DevicesChildBean
import com.itzxx.huazhun.data.model.EventMessage
import com.itzxx.huazhun.data.model.EventType
import com.itzxx.huazhun.data.repository.HttpRequestCoroutine
import com.itzxx.library_base_ktx.base.viewmodel.BaseViewModel
import com.itzxx.library_base_ktx.callback.databind.StringObservableField
import com.itzxx.library_base_ktx.callback.livedata.event.EventLiveData
import com.itzxx.library_base_ktx.ext.request
import com.itzxx.library_base_ktx.state.ListDataUiState
import org.greenrobot.eventbus.EventBus

class DeviceListViewModel : BaseViewModel() {

    var recordIdList = arrayListOf<Int>()

    //列表数据刷新
    val listDataUiStateLiveData = EventLiveData<ListDataUiState<DevicesChildBean>>()
    var runProgramObservableField = StringObservableField()

    private var cursor: String = ""

    /**
     * 获取数据列表
     */
    fun getList(isRefresh: Boolean) {
        if (isRefresh) {
            cursor = ""
            loadingChange.showDialog.postValue("数据加载中...")
        }
        request({
            HttpRequestCoroutine.detectionEquipmentPage(
                cursor,
                ""
            )
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = true,
                    isRefresh = isRefresh,
                    isEmpty = it.list.isEmpty(),
                    hasMore = !it.isLast,
                    isFirstEmpty = TextUtils.isEmpty(cursor) && it.list.isEmpty(),
                    listData = it.list
                )
            cursor = it.cursor
            listDataUiStateLiveData.postValue(listDataUiState)
            loadingChange.dismissDialog.postValue(false)
        }, {
            toast(it.errorMsg)
            loadingChange.dismissDialog.postValue(false)
        })
    }

    override fun onResumeViewModel() {
        super.onResumeViewModel()
        getList(true)
    }

    fun bindProbeDataById(devicesChildBean: DevicesChildBean) {
        if (TextUtils.isEmpty(runProgramObservableField.get())) {
            toast("填写检测程序")
            return
        }
        loadingChange.showDialog.postValue("绑定设备中...")
        request({
            HttpRequestCoroutine.bindProbeDataById(
                recordIdList,
                devicesChildBean.id.toString(),
                devicesChildBean.equipmentName,
                devicesChildBean.equipmentNo,
                runProgramObservableField.get()
            )
        }, {

            if (it) {
                loadingChange.dismissDialog.postValue(false)
                toast("关联成功！")
                val bean = EventMessage(EventType.UPLOAD_RECORD, "")
                bean.arg1 = devicesChildBean.id.toString()
                bean.arg2 = devicesChildBean.equipmentNo
                EventBus.getDefault().post(bean)
                finish()
            } else {
                toast("关联失败！")
            }
        }, {
            toast(it.errorMsg)
            loadingChange.dismissDialog.postValue(false)
        })
    }
}